<?php
Class MYSQL_DB {
	private $connet_nums = 0;	//数据库当前页面连接次数
	private $IsConnet = 0;		//数据库是否已链接
	private $link;
	function connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect = '') {
		global $dbcharset;	
			if(!$this->link=@mysqli_connect($dbhost, $dbuser, $dbpw,$dbname)) {
				$this->Err('MYSQL 连接数据库失败,请确定数据库用户名,密码设置正确<br>');
				exit;
			}	
			mysqli_query($this->link,'set names utf8');
			$this->IsConnet=1;		
		 	
	}

	
   function get_server_info(){
        global $dbhost, $dbuser, $dbpw, $dbname, $pconnect,$showerr;
    $this->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
        return mysqli_get_server_info($this->link);
    }
	
	function version(){
		return $this->get_server_info();
	}
	
	

	function close() {	
		return mysqli_close();
	}

    function array_list($SQL) {
        $query=$this->query($SQL);
        $nlist=array();
        while($rs = $this->fetch_array($query)){
            $nlist[]= $rs;
        }
        return $nlist;
    }

	function query($SQL,$method='',$showerr='1') {
		if($this->IsConnet==0){
			global $dbhost, $dbuser, $dbpw, $dbname, $pconnect;
			$this->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect);

		}

		

	

		$speed_headtime=explode(' ',microtime());
		$speed_headtime=$speed_headtime[0]+$speed_headtime[1];	
		$query = mysqli_query($this->link,$SQL);
		//分析统计查询时间

		$speed_endtime=explode(' ',microtime());
		$totaltime=number_format((($speed_endtime[0]+$speed_endtime[1]-$speed_headtime)/1),6);
		$speed_totaltime="TIME $totaltime second(s)\t$SQL\r\n";
		/*if($totaltime>0.3){
			//write_file(CHEN."/cache/MysqlTime.txt",$speed_totaltime,'a');
			//大于3M,自动删除
			if(@filesize(CHEN."/cache/MysqlTime.txt")>1024*1024*3){
				unlink(CHEN."/cache/MysqlTime.txt");
			}

		}*/

		$this->connet_nums++;
		if (!$query&&$showerr=='1')  $this->Err("数据库连接出错:$SQL<br>");
		return $query;
	}



	function get_one($SQL){
		$query=$this->query($SQL,'U_B');
		$rs =& mysqli_fetch_array($query, MYSQLI_ASSOC);
		return $rs;
	}



	function update($SQL) {
		if($this->IsConnet==0){
			global $dbhost, $dbuser, $dbpw, $dbname, $pconnect;
			$this->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
		}	
		$query = mysqli_query($this->link,$SQL);
		

		$this->connet_nums++;
		if (!$query)  $this->Err("数据库连接出错:$SQL<br>");
		return $query;
	}



	function fetch_array($query, $result_type = MYSQLI_ASSOC) {
		return mysqli_fetch_array($query, $result_type);
	}



	function num_rows($query) {
		$rows = mysqli_num_rows($query);
		return $rows;
	}


	function free_result($query) {
		return mysqli_free_result($query);
	}



	function insert_id() {
		$id = mysqli_insert_id($this->link);
		return $id;
	}



	function insert_file($file){
		global $pre;
		$readfiles=read_file($file);
		$detail=explode("\n",$readfiles);
		$count=count($detail);
		for($j=0;$j<$count;$j++){
			$ck=substr($detail[$j],0,4);
			if( ereg("#",$ck)||ereg("--",$ck) ){
				continue;
			}
			$array[]=$detail[$j];

		}
		$read=implode("\n",$array); 
		$sql=str_replace("\r",'',$read);
		$detail=explode(";\n",$sql);
		$count=count($detail);
		for($i=0;$i<$count;$i++){
			$sql=str_replace("\r",'',$detail[$i]);
			$sql=str_replace("\n",'',$sql);
			$sql=str_replace("p8_",$pre,$sql);
			$sql=trim($sql);
			if($sql){
				if(eregi("CREATE TABLE",$sql)){
					global $dbcharset;
					$sql=preg_replace("/DEFAULT CHARSET=([a-z0-9]+)/is","",$sql);
					$sql=preg_replace("/TYPE=MyISAM/is","ENGINE=MyISAM",$sql);
					if( $dbcharset && mysqli_get_server_info()>'4.1' ){
						$sql=str_replace("ENGINE=MyISAM"," ENGINE=MyISAM DEFAULT CHARSET=$dbcharset ",$sql);

					}
					if(mysqli_get_server_info()<'4.1'){
						$sql=preg_replace("/ENGINE=MyISAM/is","TYPE=MyISAM",$sql);
					}
				}
				$this->query($sql);
				$check++;
			}
		}
		return $check;
	}

	function Err($msg='') {
		$sqlerror = mysqli_error($this->link);
		$sqlerrno = mysqli_errno($this->link);
		if(strstr($sqlerror,"Can't open file: '")){
			preg_match("/Can't open file: '([^']+)\.MYI'/is",$sqlerror,$array);
			echo "系统已自动修复数据库,请再次刷新网页,如果修复不成功,请重启数据库再修复<br>";
			$this->query("REPAIR TABLE `$array[1]`");

		}

		if(strstr($sqlerror,"should be repaired")){
			$sqlerror=str_replace("\\","/",$sqlerror);
			preg_match("/([^\/]+)' is marked as/is",$sqlerror,$array);
			echo "系统已自动修复数据库,请再次刷新网页,如果修复不成功,请重启数据库再修复<br>";
			$this->query("REPAIR TABLE `$array[1]`");
		}
		echo "$msg<br>$sqlerror<br>$sqlerrno";
		//die("");
	}
}
?>